package org.example.nebula.common.exception;

import lombok.extern.slf4j.Slf4j;
import org.example.nebula.entity.out.Result;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * @author: jszhang
 * @date: 2022/12/27-11:27
 */
@Slf4j
public class GlobalExceptionHandler extends RuntimeException{

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error(e.getMessage());
        if (e.getBindingResult().getFieldError() != null) {
            return Result.error(9000, e.getBindingResult().getFieldError().getDefaultMessage());
        }
        return Result.error(9000, "参数有异常");
    }

    public GlobalExceptionHandler() {}

    public GlobalExceptionHandler(String message) {
        super(message);
    }

    public GlobalExceptionHandler(String message, Throwable cause) {
        super(message, cause);
    }

    public GlobalExceptionHandler(Throwable cause) {
        super(cause);
    }

    public GlobalExceptionHandler(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }

}
